package com.hnkywl.sts.dao.sys;

import com.hnkywl.sts.entity.sys.Cd;
import com.hnkywl.sts.entity.sys.Js;
import net.ruixin.orm.hibernate.SimpleHibernateTemplate;
import org.hibernate.SQLQuery;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.hibernate.sql.Alias;
import org.hibernate.transform.AliasToEntityMapResultTransformer;
import org.springframework.stereotype.Repository;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 说明：
 * 包:        com.hnkywl.sts.dao.sys
 * 类描述:    菜单dao类
 * 修改备注: [说明本次修改内容]
 * 版本:        [v1.0]
 */
@Repository
public class CdDao extends SimpleHibernateTemplate<Cd, Long> {
    /**
     * 查询所有根节点菜单
     *
     * @return
     */
    public List<Cd> findAllRootCd() {
        return findByCriteria(new Criterion[]{Restrictions.isNull("fcd")});
    }


    /**
     * 通过角色获得完整的菜单，包含上下级关系
     *
     * @param jsSet
     * @return
     */
    public List<Object[]> findBottomCdByJs(Set<Long> jsSet) {
        String cdSql = "select distinct cd.id  from t_sys_cd cd, t_sys_js_qx jsqx, t_sys_qx qx  where qx.cd_id = cd.id and qx.id = jsqx.qx_id and jsqx.js_id in (:jsSet)";
        SQLQuery query = createSQLQuery(cdSql);
        query.setParameterList("jsSet", jsSet);
        List list = query.list();
        return list;
    }

    /**
     * 通过角色获得完整的菜单，包含上下级关系
     *
     * @param jsSet
     * @return
     */
    public List findCdByJs(Set<Long> jsSet) {
//        String cdSql = "select id from (select distinct id,pxh from t_sys_cd where deleted!=1 and fcd_id is null start with fcd_id in (select distinct cd.fcd_id  from t_sys_cd cd, t_sys_js_qx jsqx, t_sys_qx qx  where qx.cd_id = cd.id and qx.id = jsqx.qx_id and jsqx.js_id in (:jsSet) and fcd_id is not null) connect by prior  fcd_id=id order by pxh asc)";
//        SQLQuery query = createSQLQuery(cdSql);
//        query.setParameterList("jsSet", jsSet);
//        List list = query.list();
//
//        StringBuilder sql = new StringBuilder();
//        for (int i = 0; i < list.size(); i++) {
//            sql.append("select id,fcd_id,mc,url,lx from (select distinct c.id, c.fcd_id, c.mc, c.url, c.lx,c.pxh from t_sys_cd c ");
//          //  sql.append(" where (id in (:cdlist) and c.deleted!=1) ");
//          //  sql.append(" or id in ( select distinct cd.id  from t_sys_cd cd, t_sys_js_qx jsqx, t_sys_qx qx  where cd.deleted!=1 and qx.deleted!=1 and qx.cd_id = cd.id and qx.id = jsqx.qx_id and jsqx.js_id in (:jsSet) and fcd_id is not null )");
//            sql.append(" start with id =").append(list.get(i).toString()).append(" connect by prior id = fcd_id order by fcd_id asc nulls first,pxh asc)");
//            if (i != list.size() - 1)
//                sql.append(" union all ");
//        }
//        query = createSQLQuery(sql.toString());
//      //  query.setParameterList("cdlist", list);
//       // query.setParameterList("jsSet", jsSet);
//        list = query.list();
//        return list;

        String cdSql="    select distinct id as \"id\",url as \"url\",mc as \"text\",fcd_id as \"parentId\",pxh, lx as \"type\" " +
                "          from t_sys_cd where deleted != 1 start with id in(  select distinct cd.id" +
                "          from t_sys_cd cd, t_sys_js_qx jsqx, t_sys_qx qx  where qx.cd_id = cd.id  and qx.id = jsqx.qx_id" +
                "           and jsqx.js_id in (:jsSet) and fcd_id is not null) connect by prior fcd_id = id order by pxh,id asc";
        SQLQuery query = createSQLQuery(cdSql.toString());
        query.setParameterList("jsSet", jsSet);
        query.setResultTransformer(AliasToEntityMapResultTransformer.INSTANCE);
        return query.list();
    }
}
